package by.bsu.scs.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import by.bsu.scs.model.User;
import by.bsu.scs.util.manager.CookiesManager;
import by.bsu.scs.util.manager.SessionManager;

/**
 * @author Alexey Hursevich
 * @version 1.0
 * 
 * Checks user authorization state using cookies data.
 */
public class AuthorizationFilter implements Filter {
	
	public static final String IS_AUTORIZED = "isAutorized";
	
	private String userName = null;
    private String userPassword = null;
    private String rememberSwitch = null;

    /**
     * Checks cookies for authorization data.
     * <p>
     * If user login and password is found and rememberMe flag is true -
     * user is redirected automatically to the login action, which
     * validates the data and in success case redirects user to the system
     * main page without explicit inputing the authorization data.
     * 
     * @param request
     * @param response
     * @param chain
     */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		Cookie [] cookies = ((HttpServletRequest)request).getCookies();
		if(cookies != null) {
			for(Cookie c: cookies) {
				if(CookiesManager.C_KEY_USER.equalsIgnoreCase(c.getName())) {
					userName = c.getValue();
				} 
				if(CookiesManager.C_KEY_PASS.equalsIgnoreCase(c.getName())) {
					userPassword = c.getValue();
				} 
				if(CookiesManager.C_KEY_SAVE.equalsIgnoreCase(c.getName())) {
					rememberSwitch = c.getValue();
				} 
			}
			
			User user = SessionManager.getInstance().getUser((HttpServletRequest)request);
			if(user != null) 
			{
				request.setAttribute(CookiesManager.C_KEY_USER, user.getEmail());
				request.setAttribute(CookiesManager.C_KEY_PASS,user.getPassword());
				request.setAttribute(CookiesManager.C_KEY_SAVE, rememberSwitch);
				request.setAttribute(IS_AUTORIZED, "true");
				
				request.getRequestDispatcher("/login.do?method=login").forward(request, response);
			}
			else if(userName != null && userPassword != null && rememberSwitch != null && rememberSwitch.equalsIgnoreCase("true"))
			{
				request.setAttribute(CookiesManager.C_KEY_USER, userName);
				request.setAttribute(CookiesManager.C_KEY_PASS, userPassword);
				request.setAttribute(CookiesManager.C_KEY_SAVE, rememberSwitch);
				request.setAttribute(IS_AUTORIZED, "true");
				
				request.getRequestDispatcher("/login.do?method=login").forward(request, response);
			}
		}
		chain.doFilter(request, response);	
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}